//
//  EFReturnListViewController.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/13.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFReturnListViewController.h"
#import "EFReturnListViewItem.h"
#import "EFReturnDetailViewController.h"

@interface EFReturnListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(strong,nonatomic)UIView* topView;
@property(strong,nonatomic)UILabel* titleLabel;

@property(strong,nonatomic)UITableView* tableView;
@property(nonatomic, strong)NSMutableArray *dataArray;

@property(strong,nonatomic)NSString* orderId;

@end

@implementation EFReturnListViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"退换货记录";
        self.dataArray = [NSMutableArray array];
    }
    return self;
}

-(instancetype)initWithOrderId:(NSString *)orderId{
    self = [self init];
    if(self){
        self.orderId = orderId;
    }
    return self;
}

-(void)initView{
    [self.topView addSubview:self.titleLabel];
    [self.view addSubview:self.topView];
    [self.view addSubview:self.tableView];
}

-(void)initData{
    [APP_CONTEXT.net getRetrunRecord:self.orderId OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        if(code == 0){
            if ([info objectForKey:@"returns"]) {
                NSArray *array = [info objectForKey:@"returns"];
                for (NSDictionary *dic in array) {
                    EFReturnListViewItem *item = [EFReturnListViewItem yy_modelWithDictionary:dic];
                    [self.dataArray addObject:item];
                }
                [self.tableView reloadData];
            }
            if ([info objectForKey:@"status"]) {
                NSString *status = [info objectForKey:@"status"];
                self.titleLabel.text = status;
            }
        }
    }]; 
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item = self.dataArray[indexPath.row];
    return item.viewHeight;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem* item = self.dataArray[indexPath.row];
    NSString *identifier = NSStringFromClass(item.viewClass);
    ICTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell = [[item.viewClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if ([item isKindOfClass:[ICTableViewItem class]]) {
        [cell setObject:item];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    EFReturnListViewItem* item = self.dataArray[indexPath.row];
    EFReturnDetailViewController *returnDetailVC = [[EFReturnDetailViewController alloc] initWithReturnId:item.returnId];
    [ICNavigator pushViewController:returnDetailVC animated:YES];
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kStatusBarHeight - self.topView.height)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.top = self.topView.bottom;
    }
    return _tableView;
}

-(UIView *)topView{
    if(_topView == NULL){
        _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
        _topView.backgroundColor = RGBCOLOR(0x7EAD6A);
    }
    return _topView;
}

-(UILabel *)titleLabel{
    if(_titleLabel == NULL){
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 17)];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.left = 20;
        _titleLabel.top = 18;
    }
    return _titleLabel;
}


@end
